package com.zfzs.post.modules.excel.rest;

import com.zfzs.post.aop.log.Log;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.modules.excel.domain.SysUploadExcel;
import com.zfzs.post.modules.excel.service.SysUploadExcelService;
import com.zfzs.post.modules.excel.service.dto.SysUploadExcelQueryCriteria;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 终端和交易导入
 *
 * @author ck
 * @date 2019-12-04
 */
@RestController
@RequestMapping("excel")
public class SysUploadExcelController {

    @Autowired
    private SysUploadExcelService sysUploadExcelService;

    @Log("查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_SELECT')")
    public ResponseResult getSysUploadExcels(SysUploadExcelQueryCriteria criteria, Pageable pageable) {
        return ResponseResult.ok(sysUploadExcelService.queryAll(criteria, pageable));
    }

    @Log("新增")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_CREATE')")
    public ResponseResult create(@Validated @RequestBody SysUploadExcel resources) {
        Long channelId = SecurityUtils.getUserId();
        resources.setChannelId(channelId);
        return ResponseResult.ok(sysUploadExcelService.create(resources));
    }

    @Log("修改")
    @PutMapping(value = "/update")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_EDIT')")
    public ResponseResult update(@Validated @RequestBody SysUploadExcel resources) {
        sysUploadExcelService.update(resources);
        return ResponseResult.ok();
    }

    @Log("删除")
    @DeleteMapping(value = "/delete/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_DELETE')")
    public ResponseResult delete(@PathVariable Integer id) {
        sysUploadExcelService.delete(id);
        return ResponseResult.ok();
    }
}